package com.hc.servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.hc.entity.Tenant;
import com.hc.service.TenantService;
import com.hc.serviceimp.TenantServiceImpl;

@WebServlet("/TenantServlet")
public class TenantServlet extends HttpServlet {
	//引用service层方法
	TenantService tService = new TenantServiceImpl();
	
    public TenantServlet() {
        super();
       
    }

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String action = request.getParameter("action");
		
		if (action.equals("toadd")) {
			
			request.getRequestDispatcher("/WEB-INF/tenant/add.jsp").forward(request, response);
		}
		
		
		if (action.equals("add")) {
			String tname = request.getParameter("tname");
			String tphone = request.getParameter("tphone");
			String tgender = request.getParameter("tgender");
			String taddress = request.getParameter("taddress");
			String tnumber = request.getParameter("tnumber");
			String taddtime = request.getParameter("taddtime");
			
			Tenant tenant=new Tenant(0, tname, Integer.parseInt(tphone), tgender, taddress, tnumber, taddtime, 1);
			tService.add(tenant);
			request.getRequestDispatcher("").forward(request, response);
		}
		
		if (action.equals("tolist")) {
			
			request.getRequestDispatcher("/WEB-INF/tenant/select.jsp").forward(request, response);
		}
		
		if (action.equals("list")) {
			//查询条件包括但不限于租户姓名、手机号码、性别、身份证号
			String name = request.getParameter("tname");
			String gender = request.getParameter("tgender");
			int phone = Integer.parseInt(request.getParameter("tphone"));
			String number = request.getParameter("tnumber");
			
			List<Tenant> select = tService.select(name, phone, gender, number);
			request.setAttribute("list", select);
			request.getRequestDispatcher("/WEB-INF/tenant/list.jsp").forward(request, response);
			
		}
		
		if (action.equals("toupdate")) {
			String id = request.getParameter("id");
			Tenant show = tService.show(Integer.parseInt(id));
			
			request.setAttribute("t", show);
			request.getRequestDispatcher("/WEB-INF/tenant/edit.jsp").forward(request, response);
		}
		if (action.equals("update")) {
			Tenant tenant=null;
			String tid = request.getParameter("tid");
			String tname = request.getParameter("tname");
			String tphone = request.getParameter("tphone");
			String tgender = request.getParameter("tgender");
			String taddress = request.getParameter("taddress");
			String tnumber = request.getParameter("tnumber");
			String taddtime = request.getParameter("taddtime");
			tenant=new Tenant(Integer.parseInt(tid), tname, Integer.parseInt(tphone), tgender, taddress, tnumber, taddtime, 1);
			
			tService.update(tenant);
			request.getRequestDispatcher("TenantServlet?action=list").forward(request, response);
		}
		
		if (action.equals("del")) {
			
			String id = request.getParameter("id");
			tService.del(Integer.parseInt(id));
			
			request.getRequestDispatcher("TenantServlet?action=tolist").forward(request, response);
		}
		
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		doGet(request, response);
	}

}
